home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / UXTOP.C < prev    next >
C/C++ Source or Header  |  1991-01-24  |  11KB  |  304 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/users/cph/microcode/RCS/uxtop.c,v 1.8 1991/01/24 11:26:14 cph Exp $
  4.  
  5. Copyright (c) 1990-1 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include "ux.h"
  36. #include "uxtop.h"
  37. #include "osctty.h"
  38. #include "uxutil.h"
  39. #include "errors.h"
  40. #include "option.h"
  41.  
  42. extern void EXFUN (UX_initialize_channels, (void));
  43. extern void EXFUN (UX_initialize_ctty, (int interactive));
  44. extern void EXFUN (UX_initialize_directory_reader, (void));
  45. extern void EXFUN (UX_initialize_environment, (void));
  46. extern void EXFUN (UX_initialize_processes, (void));
  47. extern void EXFUN (UX_initialize_signals, (void));
  48. extern void EXFUN (UX_initialize_terminals, (void));
  49. extern void EXFUN (UX_initialize_trap_recovery, (void));
  50. extern void EXFUN (UX_initialize_tty, (void));
  51. extern void EXFUN (UX_initialize_userio, (void));
  52.  
  53. extern void EXFUN (UX_reset_channels, (void));
  54. extern void EXFUN (UX_reset_processes, (void));
  55. extern void EXFUN (UX_reset_terminals, (void));
  56. extern void EXFUN (execute_reload_cleanups, (void));
  57.  
  58. extern void EXFUN (UX_ctty_save_external_state, (void));
  59. extern void EXFUN (UX_ctty_save_internal_state, (void));
  60. extern void EXFUN (UX_ctty_restore_internal_state, (void));
  61. extern void EXFUN (UX_ctty_restore_external_state, (void));
  62.  
  63. /* reset_interruptable_extent */
  64.  
  65. extern CONST char * OS_Name;
  66. extern CONST char * OS_Variant;
  67.  
  68. static int interactive;
  69.  
  70. int
  71. DEFUN_VOID (OS_under_emacs_p)
  72. {
  73.   return (option_emacs_subprocess);
  74. }
  75.  
  76. void
  77. DEFUN_VOID (OS_initialize)
  78. {
  79.   dstack_initialize ();
  80.   transaction_initialize ();
  81.   initialize_interruptable_extent ();
  82.   {
  83.     interactive =
  84.       (option_force_interactive
  85.        || (isatty (STDIN_FILENO))
  86.        || (isatty (STDOUT_FILENO))
  87.        || (isatty (STDERR_FILENO)));
  88.     /* If none of the stdio streams is a terminal, disassociate us
  89.        from the controlling terminal so that we're not affected by
  90.        keyboard interrupts or hangup signals.  However, if we're
  91.        running under Emacs we don't want to do this, because we want
  92.        to receive a hangup signal if Emacs dies. */
  93.     if ((!interactive) && (!option_emacs_subprocess))
  94.       UX_setsid ();
  95.     /* The argument passed to `UX_ctty_initialize' says whether to
  96.        permit interrupt control, i.e. whether to attempt to setup the
  97.        keyboard interrupt characters. */
  98.     UX_initialize_ctty (interactive);
  99.   }
  100.   UX_initialize_channels ();
  101.   UX_initialize_terminals ();
  102.   UX_initialize_environment ();
  103.   UX_initialize_tty ();
  104.   UX_initialize_userio ();
  105.   UX_initialize_signals ();
  106.   UX_initialize_processes ();
  107.   UX_initialize_trap_recovery ();
  108.   UX_initialize_directory_reader ();
  109.   OS_Name = SYSTEM_NAME;
  110.   OS_Variant = SYSTEM_VARIANT;
  111.   fprintf (stdout, "MIT Scheme running under %s\n", OS_Variant);
  112.   if ((!option_emacs_subprocess) && (OS_ctty_interrupt_control ()))
  113.     {
  114.       fputs ("", stdout);
  115.       fprintf (stdout, "Type %s followed by `H' to obtain information about interrupts.\n",
  116.            (char_description ((OS_ctty_quit_char ()), 1)));
  117.     }
  118.   fflush (stdout);
  119. #ifdef _SUNOS
  120.   vadvise (VA_ANOM);        /* Anomolous paging, don't try to guess. */
  121. #endif
  122. }
  123.  
  124. void
  125. DEFUN_VOID (OS_reset)
  126. {
  127.   /*
  128.     There should really be a reset for each initialize above,
  129.     but the rest seem innocuous.
  130.    */
  131.  
  132.   UX_reset_channels ();
  133.   UX_reset_terminals ();
  134.   UX_reset_processes ();
  135.   execute_reload_cleanups ();
  136. }
  137.  
  138. void
  139. DEFUN (OS_quit, (code, abnormal_p), int code AND int abnormal_p)
  140. {
  141.   fflush (stdout);
  142.   if (abnormal_p
  143.       && interactive
  144.       && (! ((code == TERM_SIGNAL) || (code == TERM_EOF))))
  145.     {
  146.       fputs ("\nScheme has terminated abnormally!\n", stdout);
  147.       {
  148.     int dump_core =
  149.       ((!option_disable_core_dump)
  150.        && (userio_confirm ("Would you like a core dump? [Y or N] "))
  151.        && (userio_confirm ("Do you really want a core dump? [Y or N] ")));
  152.     putc ('\n', stdout);
  153.     fflush (stdout);
  154.     if (dump_core)
  155.       UX_dump_core ();
  156.       }
  157.     }
  158.   OS_restore_external_state ();
  159. }
  160.  
  161. void
  162. DEFUN_VOID (UX_dump_core)
  163. {
  164.   OS_restore_external_state ();
  165.   /* Unmask this too? */
  166.   UX_signal (SIGABRT, SIG_DFL);
  167.   UX_abort ();
  168. }
  169.  
  170. void
  171. DEFUN_VOID (OS_save_external_state)
  172. {
  173.   UX_ctty_save_external_state ();
  174. }
  175.  
  176. void
  177. DEFUN_VOID (OS_save_internal_state)
  178. {
  179.   UX_ctty_save_internal_state ();
  180. }
  181.  
  182. void
  183. DEFUN_VOID (OS_restore_internal_state)
  184. {
  185.   UX_ctty_restore_internal_state ();
  186. }
  187.  
  188. void
  189. DEFUN_VOID (OS_restore_external_state)
  190. {
  191.   UX_ctty_restore_external_state ();
  192. }
  193.  
  194. static enum syserr_names
  195. DEFUN (error_code_to_syserr, (code), int code)
  196. {
  197.   switch (code)
  198.     {
  199.     case E2BIG:        return (syserr_arg_list_too_long);
  200.     case EACCES:    return (syserr_permission_denied);
  201.     case EAGAIN:    return (syserr_resource_temporarily_unavailable);
  202.     case EBADF:        return (syserr_bad_file_descriptor);
  203.     case EBUSY:        return (syserr_resource_busy);
  204.     case ECHILD:    return (syserr_no_child_processes);
  205.     case EDEADLK:    return (syserr_resource_deadlock_avoided);
  206.     case EDOM:        return (syserr_domain_error);
  207.     case EEXIST:    return (syserr_file_exists);
  208.     case EFAULT:    return (syserr_bad_address);
  209.     case EFBIG:        return (syserr_file_too_large);
  210.     case EINTR:        return (syserr_interrupted_function_call);
  211.     case EINVAL:    return (syserr_invalid_argument);
  212.     case EIO:        return (syserr_io_error);
  213.     case EISDIR:    return (syserr_is_a_directory);
  214.     case EMFILE:    return (syserr_too_many_open_files);
  215.     case EMLINK:    return (syserr_too_many_links);
  216.     case ENAMETOOLONG:    return (syserr_filename_too_long);
  217.     case ENFILE:    return (syserr_too_many_open_files_in_system);
  218.     case ENODEV:    return (syserr_no_such_device);
  219.     case ENOENT:    return (syserr_no_such_file_or_directory);
  220.     case ENOEXEC:    return (syserr_exec_format_error);
  221.     case ENOLCK:    return (syserr_no_locks_available);
  222.     case ENOMEM:    return (syserr_not_enough_space);
  223.     case ENOSPC:    return (syserr_no_space_left_on_device);
  224.     case ENOSYS:    return (syserr_function_not_implemented);
  225.     case ENOTDIR:    return (syserr_not_a_directory);
  226.     case ENOTEMPTY:    return (syserr_directory_not_empty);
  227.     case ENOTTY:    return (syserr_inappropriate_io_control_operation);
  228.     case ENXIO:        return (syserr_no_such_device_or_address);
  229.     case EPERM:        return (syserr_operation_not_permitted);
  230.     case EPIPE:        return (syserr_broken_pipe);
  231.     case ERANGE:    return (syserr_result_too_large);
  232.     case EROFS:        return (syserr_read_only_file_system);
  233.     case ESPIPE:    return (syserr_invalid_seek);
  234.     case ESRCH:        return (syserr_no_such_process);
  235.     case EXDEV:        return (syserr_improper_link);
  236.     default:        return (syserr_unknown);
  237.     }
  238. }
  239.  
  240. static int
  241. DEFUN (syserr_to_error_code, (syserr), enum syserr_names syserr)
  242. {
  243.   switch (syserr)
  244.     {
  245.     case syserr_arg_list_too_long:            return (E2BIG);
  246.     case syserr_bad_address:                return (EFAULT);
  247.     case syserr_bad_file_descriptor:            return (EBADF);
  248.     case syserr_broken_pipe:                return (EPIPE);
  249.     case syserr_directory_not_empty:            return (ENOTEMPTY);
  250.     case syserr_domain_error:                return (EDOM);
  251.     case syserr_exec_format_error:            return (ENOEXEC);
  252.     case syserr_file_exists:                return (EEXIST);
  253.     case syserr_file_too_large:                return (EFBIG);
  254.     case syserr_filename_too_long:            return (ENAMETOOLONG);
  255.     case syserr_function_not_implemented:        return (ENOSYS);
  256.     case syserr_improper_link:                return (EXDEV);
  257.     case syserr_inappropriate_io_control_operation:    return (ENOTTY);
  258.     case syserr_interrupted_function_call:        return (EINTR);
  259.     case syserr_invalid_argument:            return (EINVAL);
  260.     case syserr_invalid_seek:                return (ESPIPE);
  261.     case syserr_io_error:                return (EIO);
  262.     case syserr_is_a_directory:                return (EISDIR);
  263.     case syserr_no_child_processes:            return (ECHILD);
  264.     case syserr_no_locks_available:            return (ENOLCK);
  265.     case syserr_no_space_left_on_device:        return (ENOSPC);
  266.     case syserr_no_such_device:                return (ENODEV);
  267.     case syserr_no_such_device_or_address:        return (ENXIO);
  268.     case syserr_no_such_file_or_directory:        return (ENOENT);
  269.     case syserr_no_such_process:            return (ESRCH);
  270.     case syserr_not_a_directory:            return (ENOTDIR);
  271.     case syserr_not_enough_space:            return (ENOMEM);
  272.     case syserr_operation_not_permitted:        return (EPERM);
  273.     case syserr_permission_denied:            return (EACCES);
  274.     case syserr_read_only_file_system:            return (EROFS);
  275.     case syserr_resource_busy:                return (EBUSY);
  276.     case syserr_resource_deadlock_avoided:        return (EDEADLK);
  277.     case syserr_resource_temporarily_unavailable:    return (EAGAIN);
  278.     case syserr_result_too_large:            return (ERANGE);
  279.     case syserr_too_many_links:                return (EMLINK);
  280.     case syserr_too_many_open_files:            return (EMFILE);
  281.     case syserr_too_many_open_files_in_system:        return (ENFILE);
  282.     default: return (0);
  283.     }
  284. }
  285.  
  286. void
  287. DEFUN (error_system_call, (code, name), int code AND enum syscall_names name)
  288. {
  289.   extern unsigned int syscall_error_code;
  290.   extern unsigned int syscall_error_name;
  291.   syscall_error_code = ((unsigned int) (error_code_to_syserr (code)));
  292.   syscall_error_name = ((unsigned int) name);
  293.   signal_error_from_primitive (ERR_IN_SYSTEM_CALL);
  294. }
  295.  
  296. CONST char *
  297. DEFUN (OS_error_code_to_message, (syserr), unsigned int syserr)
  298. {
  299.   extern char * sys_errlist [];
  300.   extern int sys_nerr;
  301.   int code = (syserr_to_error_code ((enum syserr_names) syserr));
  302.   return (((code > 0) && (code <= sys_nerr)) ? (sys_errlist [code]) : 0);
  303. }
  304.